package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysDuty;
import cn.wgx.modules.sys.entity.SysDutyPermission;
import cn.wgx.modules.sys.services.SysDutyPermissionService;
import cn.wgx.modules.sys.services.SysDutyService;
import cn.wgx.modules.sys.services.SysPermissionService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "3.2 权限分组 - 学校教师职位")
@RestController
@RequestMapping(path = "api/sysDuty")
public class SysDutyController extends BaseController{

    @Resource
    SysDutyService service;

    @Autowired
    SysDutyPermissionService sysDutyPermissionService;

    @Autowired
    SysPermissionService sysPermissionService;

    @ApiOperation(value = "列表", notes = "" )
    @ApiPermission(false)
    @RequestMapping(path = "list", method = RequestMethod.GET)
    public Map<String, Object> getList(
            @ModelAttribute SysDuty record, 
			@RequestParam(value = "page", required = false, defaultValue = "1")Integer page,
			@RequestParam(value = "pageSize", required = false, defaultValue = "20")Integer pageSize,
            @ApiParam(value = "是否倒序,默认否")@RequestParam(defaultValue = "false", required = false)Boolean desc
        ) {
		return returnData(PageHelper.startPage(page,  Math.min(Math.abs(pageSize), maxPageSize)), service.getList(record, desc));
    }

    @ApiOperation("根据职位ID获取权限列表")
    @ApiPermission(value = false)
    @RequestMapping(path = "getPerByDutyId", method = RequestMethod.GET)
    public Map<String,Object> getPer(Integer duty_id) {
        SysDutyPermission sysDutyPermission = new SysDutyPermission();
        sysDutyPermission.setDuty_id(duty_id);
        return returnData(sysDutyPermissionService.getList(sysDutyPermission, true));
    }

	
    @ApiOperation(value = "获取单个", notes = "" )
    @ApiPermission
	@RequestMapping(path = "get", method = RequestMethod.GET)
    public Map<String, Object> getOne(Integer id) {
		return returnData(service.getOne(id));
    }

	
    @ApiOperation(value = "新建", notes = "必须参数: dutyName" )
    @ApiPermission
    @RequestMapping(path = "add", method = RequestMethod.POST)
    public Map<String, Object> insertOne(@RequestBody SysDuty record) {
        return returnData(service.insertOne(record));
    }


    @ApiOperation(value = "职位授权 -> 添加/删除权限, 一对多", notes = "必须参数: duty_id, permission_id. 数组形式,例如: [{duty_id:1, permission_id:1}]")
    @ApiPermission
    @RequestMapping(path = "updatePermission", method = RequestMethod.PATCH)
    public Map<String,Object> updateRolePermission(@ApiParam(value = "SysDutyPermission对象数组,只需duty_Id和permissionId") @RequestBody List<SysDutyPermission> sysDutyPermissions) {
        if(sysDutyPermissions == null || sysDutyPermissions.size() < 0){
            return returnErrorMap("参数错误");
        }else {
//            if(sysRolePermissions.get(0).getRoleId() == 1){
//                return returnErrorMap("管理员角色不能修改.");
//            }
        }
        //todo 动态更新权限
        Integer n = sysDutyPermissionService.updateRolePermission(sysDutyPermissions);
        if(n > 0){
            sysPermissionService.clearListCache();
        }
        return returnData(n);
    }

    @ApiOperation(value = "修改", notes = "" )
    @ApiPermission
    @RequestMapping(path = "update", method = RequestMethod.PATCH)
    public Map<String, Object> updateOne(@RequestBody SysDuty record) {
        return returnData(service.updateOne(record));

    }


    @ApiOperation(value = "删除", notes = "" )
    @ApiPermission
    @RequestMapping(path = "del/{id}", method = RequestMethod.DELETE)
    public Map<String, Object> deleteOne(@PathVariable("id") Integer id) {
        if(id <= 3){
            return returnErrorMap("固定职位不能删除");
        }
        return returnData(service.deleteOne(id));
    }

	

}